/**
 * @fileName ProjectController.java
 * @author xiaoyan.Huang
 * @email 645373484@qq.com
 * @date 上午8:54:32
 * @description TODO
 */
package com.thon.controller.project;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.thon.controller.home.HomeController;
import com.thon.controller.util.BaseController;
import com.thon.entity.project.Product;
import com.thon.entity.project.Project;
import com.thon.entity.project.ProjectBudget;
import com.thon.entity.project.ProjectSchedule;
import com.thon.entity.user.Institution;
import com.thon.service.project.ProductService;
import com.thon.service.project.ProjectBudgetService;
import com.thon.service.project.ProjectScheduleService;
import com.thon.service.project.ProjectService;
import com.thon.service.user.InstitutionService;

/**
 * @author Xiaoyan.Huang
 * @Email 645373484@qq.com
 * @filename ProjectController.java
 * @date 上午8:54:32
 */
@Controller
@RequestMapping(value="/project")
public class ProjectController  extends BaseController{

	  private static final Log log = LogFactory.getLog(HomeController.class);
	  @Autowired
	  private ProjectService projectService;
	  @Autowired
	  private ProductService productService;
	  @Autowired
	  private InstitutionService institutionService;
	  @Autowired
	  private ProjectBudgetService projectBudgetService;
	  @Autowired
	  private ProjectScheduleService projectScheduleService;
	
	   @RequestMapping("/discover")
	   public String disvover(Map<String, Object> map,HttpServletRequest request) {
		   
		   log.debug("discover");
		   
		   return "discover";
	   }
	   /*@RequestMapping(value = "/open", method = RequestMethod.GET)
		public String open(Model model) {

			return "open";
		}*/
	   @RequestMapping("/open")
	   public String open(Map<String, Object> map,HttpServletRequest request) {
		   
		   log.debug("open");
		   
		   return "open";
	   }
	   @RequestMapping(value = "/detail")
	   public String detail(Map<String, Object> map,HttpServletRequest request,Model model) {
		   log.debug("detail");
		   log.debug(request);
		   String flag=request.getParameter("flag");
		   String id = request.getParameter("id");
		   if(flag!=null){
			   Project project = projectService.getProject(Integer.parseInt(id));
				if(project.getStatus()>2 && flag.equals("true")){
					project.setViewCount(project.getViewCount()+1);
					projectService.updateProject(project);
				}
		   }
		   model.addAttribute("id", id);
		   return "detail";
	   }
	   
	   @RequestMapping(value = "/preview")
	   public String preview(Map<String, Object> map,HttpServletRequest request,Model model) {
		   String id = request.getParameter("id");
		   model.addAttribute("id", id);
		   return "preview";
	   }
	   
	   @RequestMapping(value = "/preferential/detail")
	   public String preferential_detail(Map<String, Object> map,HttpServletRequest request,Model model) {
		   log.debug("detail");
		   log.debug(request);
		   String flag=request.getParameter("flag");
		   String id = request.getParameter("id");
		   if(flag!=null){
			   Project project = projectService.getProject(Integer.parseInt(id));
				if(project.getStatus()>2 && flag.equals("true")){
					project.setViewCount(project.getViewCount()+1);
					projectService.updateProject(project);
				}
		   }
		   model.addAttribute("id", id);
		   return "preferential_detail";
	   }
	   
	   @RequestMapping(value = "/preferential/preview")
	   public String preferential_preview(Map<String, Object> map,HttpServletRequest request,Model model) {
		   String id = request.getParameter("id");
		   model.addAttribute("id", id);
		   return "preferential_preview";
	   }
	   
	   
	   @RequestMapping(value = "/edit")
	   public String addProject(HttpServletRequest request,Model model) {
		   String uid = request.getParameter("uid");
		   Institution institution = new Institution();
		   institution = institutionService.getInstitutionByUser(Integer.parseInt(uid));
		   if(institution != null){
			   Project project = new Project();
			   project = projectService.getProjectByInst(institution.getId(), 0);
			   if (project != null) {
				   model.addAttribute("project", project);
				   /*int projectId = project.getId();
				   List<ProjectSchedule> projectSchedules = projectScheduleService.listProjectSchedules(projectId);
				   List<ProjectBudget> projectBudgets = projectBudgetService.listProjectBudgets(projectId);
				   List<Product> products = productService.listProductByPro(projectId);
				   if(projectSchedules.size() > 0){
					   model.addAttribute("projectSchedules", projectSchedules);
					   model.addAttribute("projectBudgets", projectBudgets);
				   }
				   if (products.size() > 0) {
					   model.addAttribute("products", products);
				   }*/
			   }
			   
			   model.addAttribute("institution", institution);
		   }
		   
		   return "project_edit";
	   }
	   
	   
	  
	
	  
}
